/*
* shrimp7 JS
* Copyright 2009 Rens Sparrius
*
* http://www.soepgarnaal.com/shrimp7/
* http://shrimp7.googlecode.com/
*
* Licensed under the BSD license.
*/

shrimp7.facebook = shrimp7.fn.facebook = {
	timer: NULL,
	timerStarter:function(){
		shrimp7.facebook.refresh(TRUE);
	},
	me:NULL,
	notifications:NULL,
	friendsIds:NULL,
	friends:NULL,
	mailCounter:0,
	permission:{},
	streamRoot:{},
	timelineActiveStartId:0,
	setupParams:{
	
	},
	setup:function(obj){
		for(var key in obj){
			this.setupParams[key] = obj[key];
		}
		return window.shrimp7;
	},
	init:function(dataRefresh){
		if(setting("SessionKey")!==""){
			loading('facebook',TRUE,'full');
			loading('facebook',TRUE,'small');
			
			this.refresh(dataRefresh);
			
			shrimp7.facebook.getFriends();
		}
		return window.shrimp7;
	},
	refresh:function(fetch){
		// New refresh, so show the small loading image..
		loading('facebook',TRUE,'small');
		
		if(this.timer!==NULL){
			stopTimer('facebook');
		}
		
		//Get userinfo
		if(fetch){
			this.con.getUserInfo([setting("facebookUserId")],function(data){
				if(data[0]!==UNDEFINED){
					shrimp7.facebook.refreshData(TRUE,data);
				}else{
					shrimp7.facebook.refreshData(TRUE,me);
				}
				$("#facebookLoggedIn").show();
			});
			// Get notifications
			this.con.getNotifications({
				callback:function(data){
					if(data.messages!==UNDEFINED){
						shrimp7.facebook.notifications = data;
						shrimp7.facebook.refreshNotifications(data);
					}
				}
			});
		}else{
			this.refreshData(fetch,me);
			this.refreshNotifications(notifications);
			$("#facebookLoggedIn").show();
		}
		
		startTimer('facebook');
		return window.shrimp7;
	},
	refreshData:function(fetch,data){
		if(!$.isArray(data)){
			
		}else{
			loading('facebook',FALSE,'full');
			
			this.me = data;
			
			$("#userImage").attr("src",this.me[0].pic_square);
			
			var	myDate = new Date(),
				i;
			
			this.fillStatusControls(fetch);
			
			this.fetchTimeline(fetch);
		}
		return window.shrimp7;
	},
	refreshNotifications:function(nots){
		if(nots.messages!==UNDEFINED){
			if(nots.messages.unread>this.mailCounter&&setting("boolFacebookSound")){
				if(shrimp7.config.system===sidebar){
					System.Sound.playSound("sounds/gotmail.wav");
				}
				this.mailCounter = nots.messages.unread;
			}else{
				this.mailCounter = nots.messages.unread;
			}
			$("#messageCount").text(nots.messages.unread);
			$("#pokeCount").text(nots.pokes.unread);
			$("#friendRequestsCount").text(nots.friend_requests);
			if(nots.events!==UNDEFINED){
				$("#eventCount").text(nots.events.unread);
			}
		}
		return window.shrimp7;
	},
	fillStatusControls:function(fetch){
		// letop oFacebook.permission.read_stream can be undefined or FALSE.. maar door tussentijdse
		// updates van de user kan hij altijd veranderen..
		if(this.permission.read_stream!==TRUE){
			this.con.hasAppStatusPermission({
				checkFor:"read_stream",
				success:function(response){
					if(response){
						shrimp7.facebook.fetchWallData(TRUE);
					}
				}
			});
		}
		else{
			if(this.permission.read_stream){
				this.fetchWallData(fetch);
			}
		}
		return window.shrimp7;
	},
	fetchWallData:function(fetch){
		if(fetch){
			var attachment = '';
			this.con.readStream({
				source:this.me[0].uid,
				limit:30,
				root:"status",
				success:function(data){
					data = shrimp7.facebook.normalizer(data);
					shrimp7.facebook.streamRoot.status = data;
					if(data[0]!==UNDEFINED){
						attachment = shrimp7.facebook.handleStreamAttachments(data[0]);
						$("#facebookStatus").html(data[0].message+attachment);
						$("#statusComments").html('<a href="javascript:openComments(\''+data[0].id+'\','+data[0].comments+');">'+language.lngMainFacebookComments+' ('+data[0].comments+')</a>');
					}else{
						// hier werd een error msg in facebookstatus gezet, maar die moet ergens anders afgehandeld worden..
						// omdat status ook gewoon niet beschikbaar kan zijn.. dat komt omdat er een LIMIT wordt gedaan op de
						// stream list.. maar dat is handig want zo dwing je de gebruiker een nieuwe status neer te zetten..
						$("#statusComments").html('');
						$("#facebookStatus").html('No recent status found! click new and tell your friends what you\'re doing..');
					}
				}
			});
		}else{
			fillWallData(this.streamRoot.status);
		}
		return window.shrimp7;
	},
	normalizer:function(data){
		if(data.posts!==UNDEFINED){
			var	readStreamData = [],
				profiles = {},
				returnData = [],
				i,
				j,
				k,
				userId;
			
			for(j=0;j<data.profiles.length;j++){
				userId = data.profiles[j].id;
				profiles[userId] = {"userName":data.profiles[j].name,"userImage":data.profiles[j].pic_square};
			}
			
			for(i=0;i<data.posts.length;i++){
				readStreamData.push({
					id:				data.posts[i].post_id,
					user:			{
						id:		data.posts[i].actor_id
					},
					postTime:		data.posts[i].created_time,
					message:		data.posts[i].message,
					type:			data.posts[i].type,
					appId:			data.posts[i].app_id,
					attachment:		data.posts[i].attachment,
					comments:		data.posts[i].comments.count,
					likes:			data.posts[i].likes
				});
			}
			
			for(k=0;k<readStreamData.length;k++){
				if(readStreamData[k]!==UNDEFINED){
					userId = readStreamData[k].user.id;
					
					returnData[k] = readStreamData[k];
					// This fix a problem detected on 28/07/2009 when there was no profiles in the stream.get
					if(profiles[userId]!==UNDEFINED){
						returnData[k].user={
							id:				userId,
							name:			profiles[userId].userName,
							profileImgUrl:	profiles[userId].userImage
						};
					}else{
						returnData[k].user={
							name:			errorTxt,
							profileImgUrl:	'http://static.ak.facebook.com/pics/s_default.jpg'
						};
					}
				}
			}
			
			return returnData;
		}
		return window.shrimp7;
	},
	handleStreamAttachments:function(data){
		var attachment="",i,cssClass={image:"facebookStreamAttachmentImage",container:"facebookStreamAttachmentContainer"};
		if(data.attachment.media!==UNDEFINED){
			if(data.attachment.media[0]!==UNDEFINED){
				cssClass.image = "facebookStreamAttachmentImage";
				if(data.attachment.media.length>1){
					cssClass.image = "facebookStreamAttachmentImageMultiple";
					cssClass.container = "facebookStreamAttachmentContainerMultiple";
				}
				switch(data.attachment.media[0].type){
					case 'photo':
						for(i=0;i<data.attachment.media.length;i++){
							attachment = attachment+'<a href="'+data.attachment.media[i].href+'"><img src="'+data.attachment.media[i].src+'"class="'+cssClass.image+'"alt=""/></a> ';
						}
						break;
					case 'link':
						for(i=0;i<data.attachment.media.length;i++){
							attachment = attachment+'<img src="'+data.attachment.media[i].src+'"class="'+cssClass.image+'"alt=""/>';
						}
						if(data.attachment.description!==UNDEFINED){
							attachment = attachment+'<div style="clear:both;"><a href="'+data.attachment.href+'">'+data.attachment.description+'</a></div>';
						}
						break;
					case 'swf':
						for(i=0;i<data.attachment.media.length;i++){
							attachment = attachment+'<a href="'+data.attachment.media[i].swf.source_url+'"><img src="'+data.attachment.media[i].swf.preview_img+'"class="facebookStreamAttachmentImage"alt=""/></a>';
						}
						break;
					case 'video':
						for(i=0;i<data.attachment.media.length;i++){
							attachment = attachment+'<a href="'+data.attachment.media[i].video.source_url+'">Video</a>';
						}
						break;
					default:
						break;
				}
			}else if(data.attachment.description!==UNDEFINED){
					attachment = data.attachment.description;
			}
		}else{
			attachment = '';
		}
		return '<div class="'+cssClass.container+'">'+attachment+'</div>';
	},
	fetchTimeline:function(obj){
		loading('facebook',TRUE,'small');
		if(this.permission.read_stream===UNDEFINED||!this.permission.read_stream){
			this.con.hasAppStatusPermission({
				checkFor:"read_stream",
				success:function(response){
					if(!response){
						if(obj.permissionFail!==UNDEFINED){
							obj.permissionFail();
						}
					}else{
						shrimp7.facebook.fetchTimelineData({refresh:TRUE});
					}
				}
			});
		}else{
			if(this.permission.read_stream){
				this.fetchTimelineData({refresh:obj.refresh});
			}else{
				obj.permissionFail();
			}
		}
		return window.shrimp7;
	},
	fetchTimelineData:function(obj){
		if(obj.refresh){
			this.con.readStream({
				source:NULL,
				limit:30,
				root:"wall",
				success:function(data){
					shrimp7.facebook.streamRoot.wall = shrimp7.facebook.normalizer(data);
					
					shrimp7.facebook.createWallStreamHTML({
						startId:		shrimp7.facebook.timelineActiveStartId,
						dropIn:			obj.dropIn,
						data:			shrimp7.facebook.streamRoot.wall,
						pagingPrefix:	obj.pagingPrefix
					});
				}
			});
		}else{
			this.createWallStreamHTML({
				startId:		shrimp7.facebook.timelineActiveStartId,
				dropIn:			obj.dropIn,
				data:			shrimp7.facebook.streamRoot.wall,
				pagingPrefix:	obj.pagingPrefix
			});
		}
		return window.shrimp7;
	},
	/*
	* createWallStreamHTML(object)
	*
	* 
	*
	* Required:
	*
	* data			STRING
	* dropIn		STRING
	* 
	* Optional:
	*
	* dom			STRING
	* pagingPrefix	STRING
	*
	*/
	createWallStreamHTML:function(params){
		shrimp7.facebook.timelineActiveStartId=params.startId;
		
		var	dataToFilter = params.data,
			data = [];
		
		if(dataToFilter[0]!==UNDEFINED){
			var twitterC2 = String(setting('facebookStreamFilters')),
				i;
			
			for(i=0;i<dataToFilter.length;i++){
				if(twitterC2.match(dataToFilter[i].userId)===NULL){
					data.push(dataToFilter[i]);
				}
			}
			
			var	pagingValues,
				totalPerPage = 9,
				i,
				message,
				openFriendLink,
				removeLink,
				likesLink,
				attachment,
				compressedImage,
				copy2twitterLink,
				copy2friendfeedLink,
				userFilter,
				htmlStr = '',
				likeUsers,
				likesHtmlStr;
			
			pagingValues = paging(data.length,totalPerPage,params.startId);
			
			for(i=params.startId;i<pagingValues.endId;i++){
				/*
				* Handle openFriendLink
				*/
				
				if(setting("facebookUserId")!=data[i].user.id){
					openFriendLink = '<a href="javascript:facebookOpenFriend('+data[i].user.id+');">'+data[i].user.name+'</a>';
					userFilter = '<a href="javascript:facebookAddFilter('+data[i].user.id+');"><img src="images/icons/close.png"style="width:13px;"/></a>';
				}else{
					userFilter = '';
					openFriendLink = 'You';
				}
				
				/*
				* Handle remove link
				*/
				
				if(data[i].appId=='103178211049'){
					removeLink = '<a href="javascript:removePost(\''+data[i].id+'\');"><img src="images/icons/trash.png"alt=""title="Remove this post"style="vertical-align:middle;"/></a> ';
				}else{
					removeLink = '';
				}
				
				/*
				* Handle stream attachments
				*/
				
				attachment = this.handleStreamAttachments(data[i]);
				
				/*
				* Handle likes
				*/
				
				if(!data[i].likes.user_likes){
					likesLink = '<a href="javascript:addLike(\''+data[i].id+'\');"title="Send a I like this to this update"><img src="images/icons/thumbs_up.png"alt=""style="border-width:0px;width:16px;vertical-align:middle;"/></a> ';
				}else{
					likesLink = '<a href="javascript:removeLike(\''+data[i].id+'\');"title="Remove your like on this update"><img src="images/icons/thumbs_up_delete.png"alt=""style="border-width:0px;width:16px;vertical-align:middle;"/></a> ';;
				}
				
				/*
				* Handle compressed messages
				*/
				
				if(data[i].message.substr(0,2)==='.!'){
					data.text = Utf8.decode(lzw.decompress(data[i].message.substr(2)));
					compressedImage = '<img src="images/icons/lock.png"alt=""style="width:16px;"/>';
				}else{compressedImage='';}
				
				/*
				* Handle encrypted messages
				*/
				
				if(data[i].message.substr(0,2)==='.?'){
					compressedImage = '<a href="javascript:decrypt(\''+data[i].message+'\');"><img src="images/icons/lock.png"alt=""style="width:16px;border-width:0px;"/></a>';
				}else{compressedImage='';}
				
				/*
				* Copy 2 twitter
				*/
				
				if(setting("boolTwitter")&&data[i].message!==""){
					copy2twitterLink = '<a href="javascript:copy2twitter('+i+');"><img src="images/icons/twitter.png"alt=""style="width:16px;vertical-align:middle;"/></a>';
				}else{
					copy2twitterLink = '';
				}
				
				/*
				* Copy 2 friendfeed
				*/
				
				if(setting("boolFriendfeed")&&data[i].message!==""){
					copy2friendfeedLink = '<a href="javascript:copy2friendfeed('+i+');"><img src="images/icons/friendfeed.png"alt=""style="width:16px;vertical-align:middle;"/></a>';
				}else{
					copy2friendfeedLink = '';
				}
				
				/*
				* Likes
				*/
				
				likeUsers = [];
				likesHtmlStr = '';
				if(data[i].likes.sample.length>0){
					if(data[i].likes.sample.length==1){
						likesHtmlStr = '<div style="margin-left:10px;">1 user likes this</div>';
					}else{
						likesHtmlStr = '<div style="margin-left:10px;">'+data[i].likes.sample.length+' users like this</div>';
					}
				}
				
				message = parseLinks(data[i].message);
				
				message = emoticons(message);
				
				htmlStr = htmlStr+
					'<div id="f'+data[i].id.replace('_','')+'">'+
					'<div id="facebookStreamItem'+data[i].id+'-'+data[i].user.id+'-'+i+'"class="facebookStreamItem">'+
						'<div class="facebookStreamMessage"><img class="facebookStreamUserImage"src="'+data[i].user.profileImgUrl+'"alt=""/>'+message+attachment+'</div>'+
						'<div class="facebookStreamFooter">'+
							'<div class="facebookStreamInfo">'+openFriendLink+'<br/>'+relativeTime(data[i].postTime,'timestamp')+'</div>'+
							'<div class="facebookStreamOptions">'+removeLink+likesLink+'<a href="javascript:openComments(\''+data[i].id+'\','+data[i].comments+');"title="Write a comment on this update from: '+data[i].user.name+'">'+language.lngMainFacebookComments+' ('+data[i].comments+')</a>'+compressedImage+copy2twitterLink+copy2friendfeedLink+userFilter+'</div>'+
							'<div style="clear:both;"></div>'+
						'</div>'+
						'<div style="clear:both;"></div>'+
					'</div>'+likesHtmlStr+
					'</div>';
			}
			
			if(pagingValues.nextStart!==NULL){
				$(params.pagingPrefix+'Next').show().unbind('click').click(function(){
					shrimp7.facebook.createWallStreamHTML({
						startId:		pagingValues.nextStart,
						dropIn:			params.dropIn,
						data:			data,
						pagingPrefix:	params.pagingPrefix
					});
				});
			}else{
				$(params.pagingPrefix+'Next').unbind('click').hide();
			}
			if(params.startId>0){
				$(params.pagingPrefix+'Previous').show().unbind('click').click(function(){
					shrimp7.facebook.createWallStreamHTML({
						startId:		pagingValues.previousStart,
						dropIn:			params.dropIn,
						data:			data,
						pagingPrefix:	params.pagingPrefix
					});
				});
			}else{
				$(params.pagingPrefix+'Previous').unbind('click').hide();
			}
			
			// Hide possible previous error..
			$("#facebookStreamError").hide();
			
			$(params.dropIn).html(htmlStr);
			loading('facebook',FALSE,'small');
			
			$(".facebookStreamItem").width($(".facebookStreamItem").width()).draggable({appendTo:"body",containment:"document",scroll:FALSE,helper:"clone"});
		}else{
			$("#facebookStreamError").show();
			loading('facebook',FALSE,'small');
		}
		return window.shrimp7;
	},
	getFriends:function(){
		this.con.getFriendsIds({
			success:function(response){
				if(config.system===sidebar&&response.data.length>0){
					shrimp7.facebook.con.friendsIds = response.data;
					$(".facebookTotalFriends").html(response.data.length);
					shrimp7.facebook.con.getUserInfo(this.con.friendsIds,function(data){
						shrimp7.facebook.con.friends = data;
					});
				}else{
					if(config.system===web&&response.length>0){
						shrimp7.facebook.con.friendsIds = response;
						$(".facebookTotalFriends").html(response.length);
						shrimp7.facebook.con.getUserInfo(shrimp7.facebook.con.friendsIds,function(data){
							shrimp7.facebook.con.friends = data;
						});
					}
				}
			},
			error:function(){
			
			}
		});
		return window.shrimp7;
	},
	friends:function(){
		var openFriendId=NULL,
		starter=function(){
			// Direct friend open..
			if(openFriendId!==NULL){
				showFriend(openFriendId);
			}else{
				showFriends();
			}
		},
		close=function(){
			openFriendId = NULL;
		},
		showFriends=function(){
			$("#friendsContainer",activeDom).show();
			$("#friendDetail",activeDom).hide();
			switch(config.system){
				case 'sidebar':
					$("#gadgetBackground",activeDom).attr("src","images/flyout.png");
					setDimensions(450,200,'width');
					facebookShowFriends(0,"#friends",'activeDom',15,'shrimp7.facebook.friends.showFriend');
					break;
				case 'web':
					facebookShowFriends(0,"#friends",'document',15,'shrimp7.facebook.friends.showFriend');
					break;
			}
		},
		showFriend=function(g,dom){
			if(config.system===sidebar){
				$("#gadgetBackground",activeDom).attr("src","images/flyout_vertical.png");
				setDimensions(260,550,FALSE);
			}
			showFacebookFriend(g,dom);
			
			$("#friendWriteOnWallPost",activeDom).unbind(click).click(function(){
				oFacebook.writeStream($("#friendWriteOnWallTextfield",activeDom).val(),oFacebook.friends[g].uid,getSessionKey(),function(){
					
				});
			});
			$("#facebookFriendsTitle",activeDom).html(oFacebook.friends[g].name);
			$("#facebookFriendsFlyoutOptions",activeDom).prepend('<img id="backButton"src="images/icons/back.png"alt=""style="width:20px;cursor:pointer;"/> ');
			
			$("#backButton",activeDom).unbind(click).click(function(){
				$("#backButton",activeDom).remove();
				$("#friendsContainer",activeDom).show();
				$("#friendDetail",activeDom).hide();
				showFriends();
			});
			
			$("#friendsContainer",activeDom).hide();
			$("#friendDetail",activeDom).show();
		};
		return window.shrimp7;
	}
};

/*
* Facebook connector functions
*/

shrimp7.facebook.con = {
	apiKey:"460e6842edb265d791b35f4a3c5a0b4d",
	signurl:soepgarnaalURL+'shrimp7/shrimp7_sig.php',
	callingApi:'http://api.facebook.com/restserver.php',
	APISecure:'https://api.facebook.com/restserver.php',
	addSendData:function(){
		return ['call_id='+this.callId(),'api_key='+this.apiKey,'session_key='+setting("SessionKey"),'v=1.0','format='+json];
	},
	callId:function(){
		var d = new Date();
		return d.getTime()+'.00';
	},
	signature:function(paramArr,callback){
		$.ajax({
			type:		"GET",
			url:		this.signurl+"?"+paramArr.sort().join("&"),
			dataType:	"html",
			success:function(data,textStatus){
				callback(data);
			},
			error:function(XMLHttpRequest,textStatus,errorThrown){
				callback(errorTxt);
			},
			timeout: 20000
		});
		return window.shrimp7;
	},
	postWithSig:function(params,callback){
		$.ajax({
			type:"GET",
			cache:true,
			url:"http://www.soepgarnaal.com/shrimp7/web/facebookPost.php?",
			data:params,
			dataType:json,
			success:function(data){
				callback(data);
			},
			error:function(XMLHttpRequest,textStatus,errorThrown){
				callback('error');
			}
		});
	},
	getSessionKey:function(callback){
		var	con = shrimp7.facebook.con,
			sendData = ['method=facebook.auth.getSession','auth_token='+setting("facebookToken"),'api_key='+this.apiKey,'v=1.0','format='+json];
		
		if(config.system===web){
			sendData.push('callback=C');
		}
		
		this.signature(sendData,function(data){
			if(data!==errorTxt){
				sendData.push('sig='+data);
				if(config.system===web){
					$.jsonp({
						url:			con.APISecure,
						data:			sendData.join("&"),
						cache:			TRUE,
						timeout:		20000,
						success:function(json){
							callback(json);
						},
						error:function(){
							callback(errorTxt);
						}
					});
				}else{
					fbtalk(con.APISecure,sendData.join("&"),json,function(data){
						// so far so good
						if(data!==NULL){
							// success!
							callback(data);
						}else{
							callback(errorTxt);
						}
					});
				}
			}else{
				callback(errorTxt);
			}
		});
		return window.shrimp7;
	},
	hasAppStatusPermission:function(obj){
		var	con=shrimp7.facebook.con,
			sendData = ['method=facebook.users.hasAppPermission','ext_perm='+obj.checkFor];
		
		sendData.push(con.addSendData());
		
		if(config.system===web){
			sendData.push('callback=C');
		}
		
		this.signature(sendData,function(data){
			if(data!==''){
				sendData.push('sig='+data);
				
				if(config.system===web){
					$.jsonp({
						cache:		TRUE,
						timeout:	20000,
						url:		con.APISecure,
						data:		sendData.join("&"),
						success:function(json){
							obj.success(json);
						},
						error:function(json){
							if(obj.error===UNDEFINED){
								obj.error(errorTxt);
							}
						}
					});
				}else{
					fbtalk(con.callingApi,sendData.join("&"),json,function(data){
						// so far so good
						if(data!==NULL){
							if(data==1){
								con.permission[obj.checkFor] = TRUE;
								
								obj.success(TRUE);
							}
							else{
								con.permission[obj.checkFor] = FALSE;
								obj.success(FALSE);
							}
						}else{
							obj.success(FALSE);
						}
					});
				}
			}else{
				if(obj.error===UNDEFINED){
					obj.error();
				}
			}
		});
		return window.shrimp7;
	},
	getFriendsIds:function(obj){
		var con=shrimp7.facebook.con,sendData = ['method=facebook.friends.get'];
		
		sendData.push(con.addSendData());
		
		if(config.system===web){
			sendData.push('callback=C');
		}
		
		this.signature(sendData,function(data){
			if(data!==''){
				sendData.push('sig='+data);
				
				if(config.system===web){
					$.jsonp({
						url:con.APISecure,
						data:sendData.join("&"),
						cache:TRUE,
						timeout:20000,
						success:function(json){
							obj.success(json);
						},
						error:function(){
							obj.error(errorTxt);
						}
					});
				}else{
					fbtalk(con.callingApi,sendData.join("&"),json,function(data){
						// so far so good
						if(data!==NULL){
							obj.error(data);
						}
					});
				}
			}else{
				obj.error(data);
			}
		});
		return window.shrimp7;
	},
	getUserInfo:function(obj){
		var	con=shrimp7.facebook.con,
			sendData = ['method=facebook.users.getInfo','fields=uid,first_name,last_name,name,pic_small,pic_big,pic_square,pic,affiliations,profile_update_time,timezone,religion,birthday,sex,hometown_location,meeting_sex,meeting_for,relationship_status,significant_other_id,political,current_location,activities,interests,is_app_user,music,tv,movies,books,quotes,about_me,hs_info,education_history,work_history,notes_count,wall_count,status,has_added_app','uids='+userIDs.join()];
		
		sendData.push(con.addSendData());
		
		if(config.system===web){
			sendData.push('callback=C');
		}
		
		this.signature(sendData,function(data){
			if(data!==''){
				sendData.push('sig='+data);
				
				if(config.system===web){
					$.jsonp({
						url:con.APISecure,
						data:sendData.join("&"),
						cache:TRUE,
						timeout:20000,
						success:function(json){
							obj.success(json);
						},
						error:function(){alert('jo');
							obj.error(errorTxt);
						}
					});
				}else{
					fbtalk(con.callingApi,sendData.join("&"),json,function(data){
						delete con.sendData[randomRoot];
						// so far so good
						if(data!==NULL){
							if(data.error_code!==UNDEFINED){
								obj.error(errorTxt);
							}else{
								obj.success(data);
							}
						}else{
							obj.error(errorTxt);
						}
					});
				}
			}else{
				obj.error(errorTxt);
			}
		});
		return window.shrimp7;
	},
	getNotifications:function(obj){
		var	con=shrimp7.facebook.con,
			sendData = ['method=facebook.notifications.get'];
		
		sendData.push(con.addSendData());
		
		if(config.system===web){
			sendData.push('callback=C');
		}
		
		this.signature(sendData,function(data){
			if(data!==errorTxt){
				sendData.push('sig='+data);
				
				if(data!==''){
					if(config.system===web){
						$.jsonp({
							url:con.APISecure,
							data:sendData.join("&"),
							cache:TRUE,
							timeout:20000,
							success:function(json){
								obj.callback(json);
							},
							error:function(){
								obj.callback(errorTxt);
							}
						});
					}else{
						fbtalk(con.callingApi,sendData.join("&"),json,function(data){
							var errorNotifications={
								messages:"?",
								pokes:"?",
								shares:"?",
								fiend_requests:"?",
								invitations:"?",
								events:"?"
							};
							// so far so good
							if(data!==NULL){
								if(data.error_code!==UNDEFINED){
									obj.callback(errorNotifications);
								}else{
									if(data.messages!==UNDEFINED){
										obj.callback();
									}else{
										obj.callback(errorNotifications);
									}
								}
							}else{
								obj.callback(errorNotifications);
							}
						});
					}
				}
			}
		});
		return window.shrimp7;
	},
	readStream:function(obj){
		var	con=shrimp7.facebook.con,timelineItems,root=obj.root,
			sendData;
		
		if(obj.limit===NULL||!obj.limit){
			timelineItems = 2;
		}else{
			timelineItems=obj.limit;
		}
		
		sendData = ['method=facebook.stream.get','viewer_id='+setting("facebookUserId"),'limit='+timelineItems];
		
		sendData.push(con.addSendData());
		
		if(obj.source!==UNDEFINED){
			sendData.push('source_ids='+obj.source);
		}
		if(config.system===web){
			sendData.push('callback=C');
		}
		
		this.signature(sendData,function(data){
			if(data!==''){
				sendData.push('sig='+data);
				
				if(config.system===web){
					$.jsonp({
						url:		con.APISecure,
						data:		sendData.join("&"),
						cache:		TRUE,
						timeout:	20000,
						success:function(json){
							obj.success(json);
						},
						error:function(){
							obj.error(errorTxt);
						}
					});
				}else{
					fbtalk(con.APISecure,sendData.join("&"),json,function(data){
						if(data.posts!==UNDEFINED){
							obj.success(data);
						}else{
							if(data.error!==UNDEFINED){
								obj.error(errorTxt);
							}
						}
					});
				}
			}else{
				obj.error(errorTxt);
			}
		});
		return window.shrimp7;
	},
	writeStream:function(obj){
		var	con=shrimp7.facebook.con,
			sendData = ['method=facebook.stream.publish','message='+encodeURIComponent(obj.message),'uid='+setting("facebookUserId")];
		
		sendData.push(con.addSendData('writeStream'));
		
		if(obj.target_id!==UNDEFINED){
			sendData.push('target_id='+obj.target_id);
		}
		
		if(shrimp7.config.system===web){
			this.postWithSig(sendData.sort().join("&"),function(data){
				obj.success(data);
			});
		}else{
			this.signature(sendData,function(data){
				if(data!==''){
					sendData.push('sig='+data);
					fbtalk(con.APISecure,sendData.join("&"),'json',function(data){
						// so far so good
						if(data!==null){
							if(data.error_code!==UNDEFINED){
								obj.success('error');
							}else{
								obj.error('error');
							}
						}else{
							obj.error('error');
						}
					});
				}else{
					obj.error('error');
				}
			});
		}
	}
};

